package com.java110.mall.cart.storeOrderEngine;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.dto.account.AccountDto;
import com.java110.dto.gold.GoldDto;
import com.java110.dto.housekeeping.HousekeepingServDto;
import com.java110.dto.payment.PaymentPoolValueDto;
import com.java110.dto.product.ProductGroupDto;
import com.java110.dto.product.ProductSeckillDto;
import com.java110.dto.product.ProductSpecValueDto;
import com.java110.dto.shop.ShopDto;
import com.java110.dto.store.StoreDto;
import com.java110.dto.storeOrder.StoreOrderCartPriceDto;
import com.java110.dto.storeOrder.StoreOrderDto;
import com.java110.dto.storeOrder.StoreOrderReturnPayDto;
import com.java110.dto.storeOrder.StoreOrderUserRoleDto;
import com.java110.dto.storeOrder.StoreOrderCartDto;
import com.java110.dto.storeOrder.StoreOrderDiscountDto;
import com.java110.dto.storeOrder.StoreOrderLedgerAcctDto;
import com.java110.intf.acct.IAccountInnerServiceSMO;
import com.java110.intf.acct.IGoldV1InnerServiceSMO;
import com.java110.intf.acct.IPaymentPoolValueV1InnerServiceSMO;
import com.java110.intf.cart.IHousekeepingServFeeInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderDiscountInnerServiceSMO;
import com.java110.intf.cart.IStoreOrderLedgerAcctV1InnerServiceSMO;
import com.java110.intf.goods.IHousekeepingServInnerServiceSMO;
import com.java110.intf.goods.IProductGroupV1InnerServiceSMO;
import com.java110.intf.goods.IProductSeckillV1InnerServiceSMO;
import com.java110.intf.shop.IShopInnerServiceSMO;
import com.java110.intf.system.ISmallWechatV1InnerServiceSMO;
import com.java110.intf.user.IStoreV1InnerServiceSMO;
import com.java110.po.gold.GoldPo;
import com.java110.po.storeOrder.StoreOrderPo;
import com.java110.po.storeOrder.StoreOrderCartPo;
import com.java110.po.storeOrder.StoreOrderDiscountPo;
import com.java110.po.storeOrder.StoreOrderLedgerAcctPo;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.util.Assert;
import com.java110.utils.util.ListUtil;
import com.java110.utils.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * @ClassName StoreOrderComputePrice
 * @Description TODO
 * @Author wuxw
 * @Date 2022/5/21 0:47
 * @Version 1.0
 * add by wuxw 2022/5/21
 **/
@Component
public class StoreOrderComputePrice {

    @Autowired
    private IHousekeepingServInnerServiceSMO housekeepingServInnerServiceSMOImpl;

    @Autowired
    private IHousekeepingServFeeInnerServiceSMO housekeepingServFeeInnerServiceSMOImpl;

    @Autowired
    private IShopInnerServiceSMO shopInnerServiceSMOImpl;

    @Autowired
    private IStoreV1InnerServiceSMO storeInnerServiceSMOImpl;

    @Autowired
    StoreOrderProduct storeOrderProduct;


    @Autowired
    private IStoreOrderDiscountInnerServiceSMO storeOrderDiscountInnerServiceSMOImpl;

    @Autowired
    private IStoreOrderLedgerAcctV1InnerServiceSMO storeOrderLedgerAcctV1InnerServiceSMOImpl;

    @Autowired
    private IAccountInnerServiceSMO accountInnerServiceSMOImpl;

    @Autowired
    private IGoldV1InnerServiceSMO goldV1InnerServiceSMOImpl;

    @Autowired
    private ISmallWechatV1InnerServiceSMO smallWeChatInnerServiceSMOImpl;

    @Autowired
    private IProductGroupV1InnerServiceSMO productGroupV1InnerServiceSMOImpl;

    @Autowired
    private IProductSeckillV1InnerServiceSMO productSeckillV1InnerServiceSMOImpl;

    @Autowired
    private IPaymentPoolValueV1InnerServiceSMO paymentPoolValueV1InnerServiceSMOImpl;



    /**
     * 价格计算
     *
     * @param goods
     * @param storeOrderUserDto
     * @return
     */
    public StoreOrderCartPriceDto computePriceAndValidateStock(JSONObject goods, StoreOrderUserRoleDto storeOrderUserDto) {
        ProductSpecValueDto productSpecValueDto = null;
        ProductGroupDto groupBuyProductSpecDto = null;
        //判断是否为 家政服务
        String price = "";
        String prodName = "";
        String realPrice = "";
        String deduction = "0.00"; //业主抵扣金
        if (StoreOrderCartDto.CART_TYPE_PRODUCT.equals(goods.getString("cartType"))) {

            productSpecValueDto = storeOrderProduct.getProductSpecValueDto(goods);

            // 会员
            realPrice = productSpecValueDto.getPrice();
            if (StoreOrderUserRoleDto.ROLE_VIP.equals(storeOrderUserDto.getRole())
                    || StoreOrderUserRoleDto.ROLE_OWNER_VIP.equals(storeOrderUserDto.getRole())) {
                realPrice = productSpecValueDto.getVipPrice();
            } else if (StoreOrderUserRoleDto.ROLE_OWNER.equals(storeOrderUserDto.getRole())) {
                realPrice = productSpecValueDto.getOwnerPrice();
                deduction = productSpecValueDto.getDeduction();
            }
            prodName = productSpecValueDto.getProdName();

            price = getGroupKillPrice(productSpecValueDto.getProductId(), productSpecValueDto.getValueId(), realPrice, goods);


            // 将商品信息会写 goods中
            goods.put("extValueId", productSpecValueDto.getExtValueId());
            goods.put("extProdId", productSpecValueDto.getExtProdId());
            goods.put("suType", productSpecValueDto.getSuType());
            goods.put("csId", productSpecValueDto.getCsId());
        } else if (StoreOrderCartDto.CART_TYPE_SERV.equals(goods.getString("cartType"))) {
            HousekeepingServDto housekeepingServDto = new HousekeepingServDto();
            housekeepingServDto.setServId(goods.getString("productId"));
            housekeepingServDto.setShopId(goods.getString("shopId"));
            List<HousekeepingServDto> housekeepingServDtos = housekeepingServInnerServiceSMOImpl.queryHousekeepingServs(housekeepingServDto);

            Assert.listOnlyOne(housekeepingServDtos, "未找到服务");
            goods.put("areaCode", housekeepingServDtos.get(0).getAreaCode());
            prodName = housekeepingServDtos.get(0).getServName();
            price = housekeepingServFeeInnerServiceSMOImpl.queryHousekeepingServDefaultFee(housekeepingServDtos.get(0).getShopId(), housekeepingServDtos.get(0).getServId());
        }

        //数量乘以单价
        BigDecimal storeOrderCartPayPrice = new BigDecimal(Double.parseDouble(price));
        storeOrderCartPayPrice = storeOrderCartPayPrice.multiply(new BigDecimal(goods.getString("goodsNum"))).setScale(2, BigDecimal.ROUND_HALF_UP);

        //考虑优惠
        if (goods.containsKey("discountPrice") && !StringUtil.isEmpty(goods.getString("discountPrice"))) {
            double discountPrice = goods.getDouble("discountPrice");
            storeOrderCartPayPrice = storeOrderCartPayPrice.subtract(new BigDecimal(discountPrice)).setScale(2, BigDecimal.ROUND_HALF_UP);
        }

        BigDecimal storeOrderCartDeduction = new BigDecimal(Double.parseDouble(deduction));
        storeOrderCartDeduction = storeOrderCartDeduction.multiply(new BigDecimal(goods.getString("goodsNum"))).setScale(2, BigDecimal.ROUND_HALF_UP);

        StoreOrderCartPriceDto storeOrderCartPriceDto = new StoreOrderCartPriceDto();
        storeOrderCartPriceDto.setPayPrice(storeOrderCartPayPrice.doubleValue());
        storeOrderCartPriceDto.setPrice(Double.parseDouble(price));
        storeOrderCartPriceDto.setProdName(prodName);
        storeOrderCartPriceDto.setDeduction(storeOrderCartDeduction.doubleValue());
        return storeOrderCartPriceDto;
    }

    private String getGroupKillPrice(String productId, String valueId, String realPrice, JSONObject goods) {

        if (!goods.containsKey("business")) {
            return realPrice;
        }

        String business = goods.getString("business");

        if (StringUtil.isEmpty(business)) {
            return realPrice;
        }

        if (StoreOrderDto.BUSINESS_GROUP.equals(business)) {
            //查询是否 拼团产品
            ProductGroupDto productGroupDto = new ProductGroupDto();
            productGroupDto.setProductId(productId);
            productGroupDto.setValueId(valueId);
            productGroupDto.setGroupId(goods.getString("groupId"));
            List<ProductGroupDto> productGroupDtos = productGroupV1InnerServiceSMOImpl.queryProductGroups(productGroupDto);
            if (!ListUtil.isNull(productGroupDtos)) {
                return productGroupDtos.get(0).getGroupPrice();
            }
        } else if (StoreOrderDto.BUSINESS_KILL.equals(business)) {
            //查询是否 拼团产品
            ProductSeckillDto productSeckillDto = new ProductSeckillDto();
            productSeckillDto.setProductId(productId);
            productSeckillDto.setValueId(valueId);
            productSeckillDto.setKillId(goods.getString("killId"));
            List<ProductSeckillDto> productSeckillDtos = productSeckillV1InnerServiceSMOImpl.queryProductSeckills(productSeckillDto);
            if (!ListUtil.isNull(productSeckillDtos)) {
                return productSeckillDtos.get(0).getKillPrice();
            }
        }

        return realPrice;

    }

    /**
     * 计算订单费用
     *
     * @param storeOrderPo
     * @param goodsList
     */
    public StoreOrderReturnPayDto computeStoreOrderFee(StoreOrderPo storeOrderPo,
                                                       JSONArray goodsList,
                                                       List<StoreOrderCartPo> storeOrderCartPos,
                                                       StoreOrderUserRoleDto storeOrderUserDto) {

        BigDecimal orderPayPrice = new BigDecimal(0.0);
        BigDecimal orderDeduction = new BigDecimal(0.0);
        BigDecimal totalPrice = new BigDecimal(Double.parseDouble(storeOrderPo.getTotalPrice()));

        for (StoreOrderCartPo tmpStoreOrderCartPo : storeOrderCartPos) {
            orderPayPrice = orderPayPrice.add(new BigDecimal(tmpStoreOrderCartPo.getPayPrice())).setScale(2, BigDecimal.ROUND_HALF_UP);
            //只有 订单有 业主时才计算 折扣金
            if (StoreOrderUserRoleDto.ROLE_OWNER_VIP.equals(storeOrderUserDto.getRole()) || StoreOrderUserRoleDto.ROLE_OWNER.equals(storeOrderUserDto.getRole())) {
                orderDeduction = orderDeduction.add(new BigDecimal(tmpStoreOrderCartPo.getDeduction())).setScale(2, BigDecimal.ROUND_HALF_UP);
            }
            BigDecimal storeOrderCartPrice = new BigDecimal(Double.parseDouble(tmpStoreOrderCartPo.getPrice()));
            storeOrderCartPrice = storeOrderCartPrice.multiply(new BigDecimal(tmpStoreOrderCartPo.getCartNum()));
            totalPrice = totalPrice.add(storeOrderCartPrice).setScale(2, BigDecimal.ROUND_HALF_UP);
        }

        storeOrderPo.setDeduction(orderDeduction.doubleValue() + "");
        storeOrderPo.setPayPrice(orderPayPrice.doubleValue() + "");
        storeOrderPo.setTotalPrice(totalPrice.doubleValue() + "");

        if (ListUtil.isNull(storeOrderCartPos) || StringUtil.isEmpty(storeOrderCartPos.get(0).getShopId())) {
            storeOrderPo.setServiceFee("0");
            return saveLedgerAcct(storeOrderPo, goodsList, storeOrderUserDto);
        }

        //计算服务费
        ShopDto shopDto = new ShopDto();
        shopDto.setShopId(storeOrderCartPos.get(0).getShopId());
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);

        Assert.listOnlyOne(shopDtos, "商铺不存在");
        shopDto = shopDtos.get(0);
        if (StringUtil.isEmpty(shopDto.getServiceFee())) {
            storeOrderPo.setServiceFee("0");

            return saveLedgerAcct(storeOrderPo, goodsList, storeOrderUserDto);
        }

        BigDecimal serviceFeeDec = new BigDecimal(shopDto.getServiceFee());
        serviceFeeDec = serviceFeeDec.multiply(orderPayPrice).setScale(2, BigDecimal.ROUND_HALF_UP);
        storeOrderPo.setServiceFee(serviceFeeDec.doubleValue() + "");
        return saveLedgerAcct(storeOrderPo, goodsList, storeOrderUserDto);

    }

    /**
     * 保存分账
     *
     * @param storeOrderPo
     */
    public StoreOrderReturnPayDto saveLedgerAcct(StoreOrderPo storeOrderPo, JSONArray goodsList, StoreOrderUserRoleDto storeOrderUserDto) {

        StoreOrderReturnPayDto storeOrderReturnPayDto = new StoreOrderReturnPayDto();
        storeOrderReturnPayDto.setOrderId(storeOrderPo.getOrderId());
        String shopId = "-1";
        if (!ListUtil.isNull(goodsList)) {
            shopId = goodsList.getJSONObject(0).getString("shopId");
        }

        //1.0 给平台结算 主要是服务费
        ledgerAdminAcct(storeOrderPo, shopId, storeOrderReturnPayDto);
        //2.0 平台分账给物业
        ledgerPropertyAcct(storeOrderPo, storeOrderUserDto, shopId, storeOrderReturnPayDto);

        //3.0 平台分账给 商家
        ledgerShopAcct(storeOrderPo, shopId, storeOrderReturnPayDto);

        if (storeOrderReturnPayDto.getPropertyPrice() > 0 || storeOrderReturnPayDto.getAdminPrice() > 0) {
            storeOrderPo.setProfitSharing(StoreOrderDto.PROFIT_SHARING_WAIT);
        } else {
            storeOrderPo.setProfitSharing(StoreOrderDto.PROFIT_SHARING_NO);

        }
        storeOrderReturnPayDto.setPayPrice(Double.parseDouble(storeOrderPo.getPayPrice()));

        return storeOrderReturnPayDto;
    }

    /**
     * 3.0 业主优惠 都结算给物业结算
     *
     * @param storeOrderPo
     * @param storeOrderUserDto
     * @param shopId
     */
    private void ledgerPropertyAcct(StoreOrderPo storeOrderPo, StoreOrderUserRoleDto storeOrderUserDto, String shopId, StoreOrderReturnPayDto storeOrderReturnPayDto) {

        double deduction = Double.parseDouble(storeOrderPo.getDeduction());
        storeOrderReturnPayDto.setAdminPrice(deduction);
        if (deduction <= 0 || StringUtil.isEmpty(storeOrderUserDto.getOwnerId())) {
            return;
        }


        GoldDto goldDto = new GoldDto();
        goldDto.setCommunityId(storeOrderUserDto.getCommunityId());
        List<GoldDto> goldDtos = goldV1InnerServiceSMOImpl.queryGolds(goldDto);
        if (ListUtil.isNull(goldDtos)) {
            goldDtos = createGold(storeOrderUserDto);
        }
        StoreOrderLedgerAcctPo storeOrderLedgerAcctPo = new StoreOrderLedgerAcctPo();
        storeOrderLedgerAcctPo.setLaId(GenerateCodeFactory.getGeneratorId("11"));
        storeOrderLedgerAcctPo.setFeeSpecCd(StoreOrderLedgerAcctDto.FEE_SPEC_OWNER);
        storeOrderLedgerAcctPo.setAmount(storeOrderPo.getDeduction());
        storeOrderLedgerAcctPo.setOrderId(storeOrderPo.getOrderId());
        storeOrderLedgerAcctPo.setPayAcctId(goldDtos.get(0).getGoldId());
        storeOrderLedgerAcctPo.setPayAcctName(goldDtos.get(0).getGoldName());
        String ownerDeductionFlag = MappingCache.getValue("OWNER_DEDUCTION");
        if ("COUPON".equals(ownerDeductionFlag)) {
            storeOrderLedgerAcctPo.setSaveType(StoreOrderLedgerAcctDto.SAVE_TYPE_COUPON);
        } else {
            storeOrderLedgerAcctPo.setSaveType(StoreOrderLedgerAcctDto.SAVE_TYPE_GOLD);
        }
        storeOrderLedgerAcctPo.setSaveObjId(storeOrderUserDto.getOwnerId());
        storeOrderLedgerAcctPo.setShopId(shopId);
        storeOrderLedgerAcctPo.setStoreId(storeOrderUserDto.getStoreId());
        storeOrderLedgerAcctPo.setStoreName(storeOrderUserDto.getStoreName());
        int flag = storeOrderLedgerAcctV1InnerServiceSMOImpl.saveStoreOrderLedgerAcct(storeOrderLedgerAcctPo);
        if (flag < 1) {
            throw new IllegalArgumentException("分账失败");
        }
        storeOrderReturnPayDto.setPropertyMchId("");
        storeOrderReturnPayDto.setPropertyKey("");
    }


    /**
     * 分账 给商家支付的
     *
     * @param storeOrderPo
     * @param shopId
     */
    private void ledgerShopAcct(StoreOrderPo storeOrderPo, String shopId, StoreOrderReturnPayDto storeOrderReturnPayDto) {

        BigDecimal payPriceDec = new BigDecimal(storeOrderPo.getPayPrice());
        BigDecimal deductionDec = new BigDecimal(storeOrderPo.getDeduction());
        BigDecimal serviceFeeDec = new BigDecimal(storeOrderPo.getServiceFee());
        payPriceDec = payPriceDec.subtract(deductionDec).subtract(serviceFeeDec).setScale(2, BigDecimal.ROUND_HALF_UP);


        AccountDto accountDto;
        List<AccountDto> accountDtos;
        StoreOrderLedgerAcctPo storeOrderLedgerAcctPo;

        String payPlatform = MappingCache.getValue(MappingConstant.WECHAT_STORE_DOMAIN, "PAY_PLATFORM");
        String shopPayAcctId = "-1";
        String shopPayAcctName = "无";
        String key = "无";
        if (!"ON".equals(payPlatform)) {

            PaymentPoolValueDto paymentPoolValueDto = new PaymentPoolValueDto();
            paymentPoolValueDto.setShopId(shopId);
            List<PaymentPoolValueDto> paymentPoolValueDtos = paymentPoolValueV1InnerServiceSMOImpl.queryPaymentPoolValues(paymentPoolValueDto);

            if (ListUtil.isNull(paymentPoolValueDtos)) {
                throw new IllegalArgumentException("配置错误,未配置参数");
            }

             shopPayAcctId = PaymentPoolValueDto.getValue(paymentPoolValueDtos, "WECHAT_MCHID");
             key = PaymentPoolValueDto.getValue(paymentPoolValueDtos, "WECHAT_KEY");
             shopPayAcctName = PaymentPoolValueDto.getValue(paymentPoolValueDtos, "WECHAT_MCHNAME");

            storeOrderReturnPayDto.setShopPrice(payPriceDec.doubleValue());
            storeOrderReturnPayDto.setShopMchId(shopPayAcctId);
            storeOrderReturnPayDto.setShopKey(key);
        } else {
            shopPayAcctId = MappingCache.getValue(MappingConstant.WECHAT_STORE_DOMAIN, "mchId");
            shopPayAcctName = MappingCache.getValue(MappingConstant.WECHAT_STORE_DOMAIN, "mchName");
            key = MappingCache.getValue(MappingConstant.WECHAT_STORE_DOMAIN, "key");
            storeOrderReturnPayDto.setShopPrice(payPriceDec.doubleValue());
            storeOrderReturnPayDto.setShopMchId(shopPayAcctId);
            storeOrderReturnPayDto.setShopKey(key);
        }
        ShopDto shopDto = new ShopDto();
        shopDto.setShopId(shopId);
        List<ShopDto> shopDtos = shopInnerServiceSMOImpl.queryShops(shopDto);

        Assert.listOnlyOne(shopDtos, "商铺不存在");

        StoreDto storeDto = new StoreDto();
        storeDto.setStoreId(shopDtos.get(0).getStoreId());
        List<StoreDto> storeDtos = storeInnerServiceSMOImpl.queryStores(storeDto);

        Assert.listOnlyOne(storeDtos, "商家不存在");
        accountDto = new AccountDto();
        accountDto.setObjId(shopDtos.get(0).getShopId());
        accountDto.setAcctType(AccountDto.ACCT_TYPE_CASH);
        accountDtos = accountInnerServiceSMOImpl.queryAccounts(accountDto);
        if (ListUtil.isNull(accountDtos)) {
            throw new IllegalArgumentException("商家账号不存在");
        }
        //2.0 给商家结算
        storeOrderLedgerAcctPo = new StoreOrderLedgerAcctPo();
        storeOrderLedgerAcctPo.setLaId(GenerateCodeFactory.getGeneratorId("11"));
        storeOrderLedgerAcctPo.setFeeSpecCd(StoreOrderLedgerAcctDto.FEE_SPEC_PRODUCT);
        storeOrderLedgerAcctPo.setAmount(payPriceDec.doubleValue() + "");
        storeOrderLedgerAcctPo.setOrderId(storeOrderPo.getOrderId());
        storeOrderLedgerAcctPo.setPayAcctId(shopPayAcctId);
        storeOrderLedgerAcctPo.setPayAcctName(shopPayAcctName);
        storeOrderLedgerAcctPo.setSaveType(StoreOrderLedgerAcctDto.SAVE_TYPE_ACCT);
        storeOrderLedgerAcctPo.setSaveObjId(accountDtos.get(0).getAcctId());
        storeOrderLedgerAcctPo.setShopId(shopId);
        storeOrderLedgerAcctPo.setStoreId(shopDtos.get(0).getStoreId());
        storeOrderLedgerAcctPo.setStoreName(storeDtos.get(0).getName());
        int flag = storeOrderLedgerAcctV1InnerServiceSMOImpl.saveStoreOrderLedgerAcct(storeOrderLedgerAcctPo);
        if (flag < 1) {
            throw new IllegalArgumentException("分账失败");
        }


    }

    // 给运营分成
    private void ledgerAdminAcct(StoreOrderPo storeOrderPo, String shopId, StoreOrderReturnPayDto storeOrderReturnPayDto) {

        double serviceFee = Double.parseDouble(storeOrderPo.getServiceFee());
        double deduction = Double.parseDouble(storeOrderPo.getDeduction());
        serviceFee = new BigDecimal(serviceFee).add(new BigDecimal(deduction)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        storeOrderReturnPayDto.setAdminPrice(serviceFee);
        if (serviceFee <= 0) {
            return;
        }
        AccountDto accountDto = new AccountDto();
        accountDto.setObjId(StoreDto.STORE_ADMIN);
        accountDto.setAcctType(AccountDto.ACCT_TYPE_CASH);
        List<AccountDto> accountDtos = accountInnerServiceSMOImpl.queryAccounts(accountDto);
        Assert.listOnlyOne(accountDtos, "运营账号不存在");

        StoreOrderLedgerAcctPo storeOrderLedgerAcctPo = new StoreOrderLedgerAcctPo();
        storeOrderLedgerAcctPo.setLaId(GenerateCodeFactory.getGeneratorId("11"));
        storeOrderLedgerAcctPo.setFeeSpecCd(StoreOrderLedgerAcctDto.FEE_SPEC_SERVICE_FEE);
        storeOrderLedgerAcctPo.setAmount(storeOrderPo.getServiceFee());
        storeOrderLedgerAcctPo.setOrderId(storeOrderPo.getOrderId());
        storeOrderLedgerAcctPo.setPayAcctId(MappingCache.getValue(MappingConstant.WECHAT_STORE_DOMAIN, "mchId"));
        storeOrderLedgerAcctPo.setPayAcctName(MappingCache.getValue(MappingConstant.WECHAT_STORE_DOMAIN, "mchName"));
        storeOrderLedgerAcctPo.setSaveType(StoreOrderLedgerAcctDto.SAVE_TYPE_ACCT);
        storeOrderLedgerAcctPo.setSaveObjId(accountDtos.get(0).getAcctId());
        storeOrderLedgerAcctPo.setShopId(shopId);
        storeOrderLedgerAcctPo.setStoreId(StoreDto.STORE_ADMIN);
        storeOrderLedgerAcctPo.setStoreName("运营团队");
        int flag = storeOrderLedgerAcctV1InnerServiceSMOImpl.saveStoreOrderLedgerAcct(storeOrderLedgerAcctPo);
        if (flag < 1) {
            throw new IllegalArgumentException("分账失败");
        }

        storeOrderReturnPayDto.setAdminKey(MappingCache.getValue(MappingConstant.WECHAT_STORE_DOMAIN, "key"));
        storeOrderReturnPayDto.setAdminMchId(MappingCache.getValue(MappingConstant.WECHAT_STORE_DOMAIN, "mchId"));
    }

    /**
     * 订单优惠金额
     *
     * @param storeOrder
     * @param storeOrderPo
     * @param storeOrderCarts
     */
    public void dealOrderDiscountPrice(JSONObject storeOrder, StoreOrderPo storeOrderPo, JSONArray storeOrderCarts) {

        if (!storeOrder.containsKey("discountPrice") || StringUtil.isEmpty(storeOrder.getString("discountPrice"))) {
            return;
        }

        double discountPrice = storeOrder.getDouble("discountPrice");
        if (discountPrice == 0) { // 没有优惠
            return;
        }


        JSONObject goods = storeOrderCarts.getJSONObject(0);

        StoreOrderDiscountPo storeOrderDiscountPo = new StoreOrderDiscountPo();
        storeOrderDiscountPo.setDiscountId(GenerateCodeFactory.getGeneratorId(GenerateCodeFactory.CODE_PREFIX_discountId));
        storeOrderDiscountPo.setDiscountObjId(storeOrderPo.getOrderId());
        storeOrderDiscountPo.setDiscountObjType(StoreOrderDiscountDto.DISCOUNT_OBJ_TYPE_ORDER);
        storeOrderDiscountPo.setDiscountPrice(discountPrice + "");
        storeOrderDiscountPo.setDiscountRate(storeOrder.getString("discountRate"));
        storeOrderDiscountPo.setDiscountType(storeOrder.getString("discountType"));
        storeOrderDiscountPo.setShopId(goods.getString("shopId"));
        int flag = storeOrderDiscountInnerServiceSMOImpl.saveStoreOrderDiscount(storeOrderDiscountPo);
        if (flag < 1) {
            throw new IllegalArgumentException("记录优惠信息失败");
        }
    }

    private List<GoldDto> createGold(StoreOrderUserRoleDto storeOrderUserDto) {

        GoldPo goldPo = new GoldPo();
        goldPo.setAmount("0");
        goldPo.setGoldName(storeOrderUserDto.getCommunityName());
        goldPo.setGoldId(GenerateCodeFactory.getGeneratorId("11"));
        goldPo.setCommunityName(storeOrderUserDto.getCommunityName());
        goldPo.setStoreName(storeOrderUserDto.getStoreName());
        goldPo.setCommunityId(storeOrderUserDto.getCommunityId());
        goldPo.setStoreId(storeOrderUserDto.getStoreId());
        goldV1InnerServiceSMOImpl.saveGold(goldPo);

        GoldDto goldDto = new GoldDto();
        goldDto.setCommunityId(storeOrderUserDto.getCommunityId());
        List<GoldDto> goldDtos = goldV1InnerServiceSMOImpl.queryGolds(goldDto);
        return goldDtos;
    }

}
